// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <vector>

#include <include/args.h>
#include <include/paddleocr.h>
#include <include/paddlestructure.h>

using namespace PaddleOCR;

void check_params() {
    if (FLAGS_det) {
        if (FLAGS_det_model_dir.empty() || FLAGS_image_dir.empty()) {
            std::cout << "Usage[det]: ./ppocr "
                         "--det_model_dir=/PATH/TO/DET_INFERENCE_MODEL/ "
                      << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
            exit(1);
        }
    }
    if (FLAGS_rec) {
        std::cout
                << "In PP-OCRv3, rec_image_shape parameter defaults to '3, 48, 320',"
                   "if you are using recognition model with PP-OCRv2 or an older "
                   "version, "
                   "please set --rec_image_shape='3,32,320"
                << std::endl;
        if (FLAGS_rec_model_dir.empty() || FLAGS_image_dir.empty()) {
            std::cout << "Usage[rec]: ./ppocr "
                         "--rec_model_dir=/PATH/TO/REC_INFERENCE_MODEL/ "
                      << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
            exit(1);
        }
    }
    if (FLAGS_cls && FLAGS_use_angle_cls) {
        if (FLAGS_cls_model_dir.empty() || FLAGS_image_dir.empty()) {
            std::cout << "Usage[cls]: ./ppocr "
                      << "--cls_model_dir=/PATH/TO/REC_INFERENCE_MODEL/ "
                      << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
            exit(1);
        }
    }
    if (FLAGS_table) {
        if (FLAGS_table_model_dir.empty() || FLAGS_det_model_dir.empty() ||
                FLAGS_rec_model_dir.empty() || FLAGS_image_dir.empty()) {
            std::cout << "Usage[table]: ./ppocr "
                      << "--det_model_dir=/PATH/TO/DET_INFERENCE_MODEL/ "
                      << "--rec_model_dir=/PATH/TO/REC_INFERENCE_MODEL/ "
                      << "--table_model_dir=/PATH/TO/TABLE_INFERENCE_MODEL/ "
                      << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
            exit(1);
        }
    }
    if (FLAGS_layout) {
        if (FLAGS_layout_model_dir.empty() || FLAGS_image_dir.empty()) {
            std::cout << "Usage[layout]: ./ppocr "
                      << "--layout_model_dir=/PATH/TO/LAYOUT_INFERENCE_MODEL/ "
                      << "--image_dir=/PATH/TO/INPUT/IMAGE/" << std::endl;
            exit(1);
        }
    }
    if (FLAGS_precision != "fp32" && FLAGS_precision != "fp16" &&
            FLAGS_precision != "int8") {
        std::cout << "precison should be 'fp32'(default), 'fp16' or 'int8'. "
                  << std::endl;
        exit(1);
    }
}

std::vector<std::vector<OCRPredictResult>> ocr_results_img(cv::Mat &img) {
    PPOCR ocr;
    std::vector<cv::Mat> img_list;
    if (!img.data) {
        std::cerr << "[ERROR] image pixmap: "
                  << std::endl;
        std::vector<std::vector<OCRPredictResult>> ocr_results;
        return ocr_results;
    }
    img_list.push_back(img);

    std::vector<std::vector<OCRPredictResult>> ocr_results =
            ocr.ocr(img_list, FLAGS_det, FLAGS_rec, FLAGS_cls);
    Utility::print_result(ocr_results[0]);
    return ocr_results;
}

std::vector<std::vector<OCRPredictResult>> ocr_results(cv::String &cv_img_name) {
    PPOCR ocr;
    std::vector<cv::Mat> img_list;

    cv::Mat img = cv::imread(cv_img_name, cv::IMREAD_COLOR);
    if (!img.data) {
        std::cerr << "[ERROR] image read failed! image path: "
                  << cv_img_name << std::endl;
        std::vector<std::vector<OCRPredictResult>> ocr_results;
        return ocr_results;
    }
    img_list.push_back(img);



    std::vector<std::vector<OCRPredictResult>> ocr_results =
            ocr.ocr(img_list, FLAGS_det, FLAGS_rec, FLAGS_cls);
    Utility::print_result(ocr_results[0]);
    return ocr_results;
}

void ocr(std::vector<cv::String> &cv_all_img_names) {
    PPOCR ocr;

    if (FLAGS_benchmark) {
        ocr.reset_timer();
    }

    std::vector<cv::Mat> img_list;
    std::vector<cv::String> img_names;
    for (int i = 0; i < cv_all_img_names.size(); ++i) {
        cv::Mat img = cv::imread(cv_all_img_names[i], cv::IMREAD_COLOR);
        if (!img.data) {
            std::cerr << "[ERROR] image read failed! image path: "
                      << cv_all_img_names[i] << std::endl;
            continue;
        }
        img_list.push_back(img);
        img_names.push_back(cv_all_img_names[i]);
    }

    std::vector<std::vector<OCRPredictResult>> ocr_results =
            ocr.ocr(img_list, FLAGS_det, FLAGS_rec, FLAGS_cls);

    for (int i = 0; i < img_names.size(); ++i) {
        std::cout << "predict img: " << cv_all_img_names[i] << std::endl;
        Utility::print_result(ocr_results[i]);
        if (FLAGS_visualize && FLAGS_det) {
            std::string file_name = Utility::basename(img_names[i]);
            cv::Mat srcimg = img_list[i];
            Utility::VisualizeBboxes(srcimg, ocr_results[i],
                                     FLAGS_output + "/" + file_name);
        }
    }
    if (FLAGS_benchmark) {
        ocr.benchmark_log(cv_all_img_names.size());
    }
}

void structure(std::vector<cv::String> &cv_all_img_names) {
    PaddleOCR::PaddleStructure engine;

    if (FLAGS_benchmark) {
        engine.reset_timer();
    }

    for (int i = 0; i < cv_all_img_names.size(); i++) {
        std::cout << "predict img: " << cv_all_img_names[i] << std::endl;
        cv::Mat img = cv::imread(cv_all_img_names[i], cv::IMREAD_COLOR);
        if (!img.data) {
            std::cerr << "[ERROR] image read failed! image path: "
                      << cv_all_img_names[i] << std::endl;
            continue;
        }

        std::vector<StructurePredictResult> structure_results = engine.structure(
                    img, FLAGS_layout, FLAGS_table, FLAGS_det && FLAGS_rec);

        for (int j = 0; j < structure_results.size(); j++) {
            std::cout << j << "\ttype: " << structure_results[j].type
                      << ", region: [";
            std::cout << structure_results[j].box[0] << ","
                                                     << structure_results[j].box[1] << ","
                                                     << structure_results[j].box[2] << ","
                                                     << structure_results[j].box[3] << "], score: ";
            std::cout << structure_results[j].confidence << ", res: ";

            if (structure_results[j].type == "table") {
                std::cout << structure_results[j].html << std::endl;
                if (structure_results[j].cell_box.size() > 0 && FLAGS_visualize) {
                    std::string file_name = Utility::basename(cv_all_img_names[i]);

                    Utility::VisualizeBboxes(img, structure_results[j],
                                             FLAGS_output + "/" + std::to_string(j) +
                                             "_" + file_name);
                }
            } else {
                std::cout << "count of ocr result is : "
                          << structure_results[j].text_res.size() << std::endl;
                if (structure_results[j].text_res.size() > 0) {
                    std::cout << "********** print ocr result "
                              << "**********" << std::endl;
                    Utility::print_result(structure_results[j].text_res);
                    std::cout << "********** end print ocr result "
                              << "**********" << std::endl;
                }
            }
        }
    }
    if (FLAGS_benchmark) {
        engine.benchmark_log(cv_all_img_names.size());
    }
}
std::vector<OCRPredictResult> ocr_img(cv::Mat img)
{
    char fake_argv[8][256];
    sprintf(fake_argv[0], "ppocr");
    sprintf(fake_argv[1], "--det_model_dir=/usr/share/live-image-text/inference/det_db");
    sprintf(fake_argv[2], "--rec_model_dir=/usr/share/live-image-text/inference/rec_rcnn");
    sprintf(fake_argv[3], "--cls_model_dir=/usr/share/live-image-text/inference/cls");
    sprintf(fake_argv[4], "--use_angle_cls=true");
    sprintf(fake_argv[5], "--det=true");
    sprintf(fake_argv[6], "--rec=true");
    sprintf(fake_argv[7], "--cls=true");

    int fake_argc = 8;
    char **fake_argv2 = new char *[24]{fake_argv[0], fake_argv[1], fake_argv[2], fake_argv[3],fake_argv[4], fake_argv[5], fake_argv[6], fake_argv[7]};

    google::ParseCommandLineFlags(&fake_argc, &fake_argv2, true);
//    check_params();



    std::vector<std::vector<OCRPredictResult>> results = ocr_results_img(img);
    if(results.size())
    {
        printf("get ocr_result");
        return results[0];
    }
    else
    {
         printf("no ocr_result");
        std::vector<OCRPredictResult> ocr_result;
        return ocr_result;
    }
}
std::vector<OCRPredictResult> ocr_file(const char *file) {
    // Parsing command-line
    char fake_argv[9][256];
    sprintf(fake_argv[0], "ppocr");
    sprintf(fake_argv[1], "--det_model_dir=/usr/share/live-image-text/inference/det_db");
    sprintf(fake_argv[2], "--rec_model_dir=/usr/share/live-image-text/inference/rec_rcnn");
    sprintf(fake_argv[3], "--cls_model_dir=/usr/share/live-image-text/inference/cls");
    sprintf(fake_argv[4], "--image_dir=%s",file);
    sprintf(fake_argv[5], "--use_angle_cls=true");
    sprintf(fake_argv[6], "--det=true");
    sprintf(fake_argv[7], "--rec=true");
    sprintf(fake_argv[8], "--cls=true");

    int fake_argc = 9;
    char **fake_argv2 = new char *[24]{fake_argv[0], fake_argv[1], fake_argv[2], fake_argv[3],fake_argv[4], fake_argv[5], fake_argv[6], fake_argv[7], fake_argv[8]};

    google::ParseCommandLineFlags(&fake_argc, &fake_argv2, true);
    check_params();

    if (!Utility::PathExists(FLAGS_image_dir)) {
    std::cerr << "[ERROR] image path not exist! image_dir: " << FLAGS_image_dir
              << std::endl;
    exit(1);
    }

    std::vector<cv::String> cv_all_img_names;
    cv::glob(FLAGS_image_dir, cv_all_img_names);
    std::cout << "total images num: " << cv_all_img_names.size() << std::endl;

    if (!Utility::PathExists(FLAGS_output)) {
        Utility::CreateDir(FLAGS_output);
    }

    std::vector<std::vector<OCRPredictResult>> results = ocr_results(cv_all_img_names[0]);
    if(results.size())
    {
        return results[0];
    }
    else
    {
        std::vector<OCRPredictResult> ocr_result;
        return ocr_result;
    }
}
